package com.spring.crm.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring.crm.conf.CommonResult;
import com.spring.crm.conf.ResultCode;
import com.spring.crm.entity.Linkman;
import com.spring.crm.mapper.LinkmanMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何卓
 * @since 2022-01-18
 */
@RestController
@RequestMapping("/api/crm/linkman")
public class LinkmanController {

    @Resource
    private LinkmanMapper linkmanMapper;

    @CrossOrigin(origins = "*")
    @PostMapping("insert")
    public CommonResult insertOne(@RequestBody Linkman record) {
        CommonResult rs;
        try {
//            if(record.getStartTime()==null){
//                return CommonResult.failed("名称不能为空");
//            }
            //LocalDateTime dd = LocalDateTime.now(); // 注意是util包下的Date
//            record.setCreateTime(dd.toString());
            int ret = linkmanMapper.insert(record);
            if (ret != 1){
                rs = CommonResult.failed();
            }else {
                JSONObject data = new JSONObject();
                data.put("linkmanId",record.getLinkmanId());
                rs = CommonResult.success(data);
            }
        }catch(Exception ex){
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }

    @CrossOrigin(origins = "*")
    @GetMapping("condition")
    public CommonResult selectByCondition(@RequestParam Map<String,String> map) {
        CommonResult rs;
        try {
            QueryWrapper<Linkman> queryWrapper = new QueryWrapper<>();
            if(map.containsKey("linkmanId")){
                queryWrapper.eq("linkman_id",map.get("linkmanId"));
            }
            if(map.containsKey("customId")){
                queryWrapper.eq("custom_id",map.get("customId"));
            }
            if(map.containsKey("name")){
                queryWrapper.eq("name",map.get("name"));
            }
            //queryWrapper.orderByAsc("num");
            Page<Linkman> page = new Page<>(Long.valueOf(map.get("current")),Long.valueOf(map.get("size")));
            IPage<Linkman> SalesIPage = linkmanMapper.selectPage(page,queryWrapper);
            Integer selectCount = Math.toIntExact(linkmanMapper.selectCount(queryWrapper));
            List<Linkman> list = SalesIPage.getRecords();
            rs = CommonResult.success(list,selectCount);
        }catch(Exception ex){
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }

    @CrossOrigin(origins = "*")
    @DeleteMapping ("delete")
    public CommonResult deleteOne(@RequestBody Linkman record) {
        CommonResult rs;
        try {
            Linkman linkman = linkmanMapper.selectById(record.getLinkmanId());
            if(linkman==null){
                return CommonResult.failed(ResultCode.DATA_Not_Exist_ERROR);
            }
            int ret = linkmanMapper.deleteById(record.getLinkmanId());
            if (ret != 1) {
                rs = CommonResult.failed();
            } else {
                rs = CommonResult.success("");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }

    @CrossOrigin(origins = "*")
    @PutMapping("update")
    public CommonResult updateOne(@RequestBody Linkman record) {
        CommonResult rs;
        try {
            Linkman linkman = linkmanMapper.selectById(record.getLinkmanId());
            if(linkman==null){
                return CommonResult.failed(ResultCode.DATA_Not_Exist_ERROR);
            }
            int ret = linkmanMapper.updateById(record);
            if (ret != 1) {
                rs = CommonResult.failed();
            } else {
                JSONObject data = new JSONObject();
                data.put("linkmanId", record.getLinkmanId());
                rs = CommonResult.success(data);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }
}
